package light.head.dao.el;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Service;

import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.model.el.PaperTemplateItem;
@Service
public class PaperTemplateItemDAO extends BaseDAO<PaperTemplateItem, Integer>{
	/**
	 * 根据模板编号获取明细
	 */
	public List<PaperTemplateItem> findByTtc(Integer tc){
		String sql = "FROM PaperTemplateItem ti WHERE ti.deleted = 0 AND ti.ttc = ?";
		return find(sql, tc);
	}

	public RESULT update(PaperTemplateItem item){
		if(item == null || item.getCode() == null) return RESULT.ERROR;
		
		PaperTemplateItem old = get(item.getCode());
		
		if(old == null) return RESULT.NOT_FIND;
		
		if(item.getType() != null && item.getType().intValue() != old.getType().intValue()){
			old.setType(item.getType());
		}
		if(item.getLc() != null && item.getLc().intValue() != old.getLc().intValue()){
			old.setLc(item.getLc());
		}
		if(item.getNum() != null && item.getNum() != old.getNum()){
			old.setNum(item.getNum());
		}
		if(item.getPoint() != null && item.getPoint() != old.getPoint()){
			old.setPoint(item.getPoint());
		}
		return upd(old);
	}

	/**
	 * 去掉自选题后的模板明细
	 * clc	班级编号
	 * ttc	模板编号
	 * cc	课程编号
	 * @return
	 */
	public List<PaperTemplateItem> findByClcAndTtcAndCc(Integer clc, Integer ttc, Integer cc){
		if(clc == null || ttc == null || cc == null) return null;
		List<Object> parmList = new ArrayList<Object>();
		StringBuffer sqlBuffer = new StringBuffer("SELECT tti.code, tti.ttc, tti.type, tti.lc, tti.point,");
		sqlBuffer.append(" (tti.num - IFNULL((SELECT COUNT(*) FROM ClassCourseTopicRelation cctr WHERE cctr.clc = ? AND cctr.cc = ? AND cctr.tic = tti.code), 0)) as num");
		sqlBuffer.append(" FROM PaperTemplateItem tti");
		sqlBuffer.append(" WHERE tti.deleted = 0 AND tti.ttc = ?");
		parmList.add(clc);
		parmList.add(cc);
		parmList.add(ttc);
		
		final String sql = sqlBuffer.toString();
		final List<Object> parm = parmList;
		
		return this.getHibernateTemplate().execute(new HibernateCallback<List<PaperTemplateItem>>() {

			@Override
			public List<PaperTemplateItem> doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				SQLQuery query = session.createSQLQuery(sql);
				for(int i = 0; i < parm.size(); i++){
					query.setParameter(i, parm.get(i));
				}
				
				@SuppressWarnings("unchecked")
				List<Object[]> result = query.list();
				if(result != null){
					List<PaperTemplateItem> list = new ArrayList<PaperTemplateItem>();
					PaperTemplateItem ti = null;
					for(Object[] item : result){
						ti = new PaperTemplateItem();
						ti.setCode((Integer)item[0]);
						ti.setTtc((Integer)item[1]);
						ti.setType((Byte)item[2]);
						ti.setLc((Integer)item[3]);
						ti.setPoint((Float)item[4]);
						ti.setNum(item[5] != null && ((BigInteger)item[5]).shortValue() > 0 ? ((BigInteger)item[5]).shortValue() : 0);
						list.add(ti);
					}
					return list;
				}
				return null;
			}
		});
	}
}
